﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;
using System.Data;

namespace PresentationLayer
{
    public partial class MakeRequisition : System.Web.UI.Page
    {

        MakeRequistionController mrContoller;
        StationeryInventoryEntities1 ent;
        DataTable dtCurrentTable;
        DataRow drCurrentRow = null;
        String userName = "Employee2";

        protected void Page_Load(object sender, EventArgs e)
        {
            mrContoller = new MakeRequistionController();
            ent = new StationeryInventoryEntities1();
            if (!IsPostBack)
            {
                

                ddlCategory.DataSource = mrContoller.getCatList();
                ddlCategory.DataTextField = "CategoryName";
                ddlCategory.DataValueField = "CategoryID";
                ddlCategory.DataBind();
                ddlCategory.Items.Insert(0, "Choose Category...");
                

                //---------------------for tempItems-----------------------
                DataTable dt = new DataTable();

                dt.Columns.Add("ItemCode", typeof(string));
                dt.Columns.Add("ItemName", typeof(string));
                dt.Columns.Add("Category", typeof(string));
                dt.Columns.Add("Qty", typeof(int));

                ViewState["CurrentTable"] = dt;

                gvTempItemList.DataSource = dt;
                gvTempItemList.DataBind();
                //-----------------------end for tempItems------------------

            }
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            int catId = Convert.ToInt32(ddlCategory.SelectedValue.ToString());
            ddlStationery.DataSource = mrContoller.getStationeryListByCatID(catId);
            ddlStationery.DataTextField = "ItemName";
            ddlStationery.DataValueField = "ItemCode";
            ddlStationery.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            

            if (ViewState["CurrentTable"] != null)
            {
                dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            }
            else
            {
                dtCurrentTable = new DataTable();
            }
            drCurrentRow = dtCurrentTable.NewRow();
            drCurrentRow[0] = ddlStationery.SelectedValue;
            drCurrentRow[1] = ddlStationery.SelectedItem.Text;
            drCurrentRow[2] = ddlCategory.SelectedItem.Text;
            drCurrentRow[3] = txtQty.Text;

            dtCurrentTable.Rows.Add(drCurrentRow);
            ViewState["CurrentTable"] = dtCurrentTable;
            gvTempItemList.DataSource = dtCurrentTable;
            gvTempItemList.DataBind();

            //after adding, clear all field
            txtQty.Text = "";
            ddlCategory.SelectedIndex = 0;
            ddlStationery.Items.Clear();
            
        }

        protected void gvTempItemList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            if (ViewState["CurrentTable"] != null)
            {
                dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                dtCurrentTable.Rows.RemoveAt(e.RowIndex);
                gvTempItemList.DataSource = dtCurrentTable;
                gvTempItemList.DataBind();
            }

         
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //int reqID;

            String itemCode;
            int reqQty;

            //for requisition table
            int reqBy = mrContoller.getEmpIDFromUserName(userName);
            int reqID = mrContoller.makeRequisition(reqBy);

            for (int i = 0; i < gvTempItemList.Rows.Count; i++)
            {
                itemCode = gvTempItemList.Rows[i].Cells[0].Text;
                reqQty = Convert.ToInt32(gvTempItemList.Rows[i].Cells[3].Text);
                mrContoller.makeRequistionDetail(reqID, itemCode, reqQty);
            }


            mrContoller.SendNotificationToDeptHead(userName,reqID);

            Response.Redirect("~/RequisitionSuccess.aspx");
            
            string script = "alert('Your Stationery Requisition have been submitted successfully.\n An email has been sent to the Department Head!');";
            ClientScript.RegisterStartupScript(this.GetType(), "Information", script, true);
        }


        
        
    }
}